home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9451 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: "Stephen C. Marney" <scmarney@erols.com>
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: Simple inheritance Q
  5. Date: 1 Mar 1996 13:31:02 -0000
  6. Organization: Techsyn Company
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: herbs@connobj.com
  9. Message-ID: <4h6u6m$qto@netlab.cs.rpi.edu>
  10. References: <4gumo0$hta@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12. X-Original-Date: Thu, 29 Feb 1996 23:44:54 -0500
  13.  
  14. Eric Simon wrote:
  15. > I have a base class, called Animal, and a derived class, called Cat.
  16. > In my main program I have:
  17. > 1.    Animal *p;
  18. > 2.    Cat a(args);
  19. > 3.    p = &a;
  20. > Now, if I define
  21. >     class Cat:Animal{...
  22. > then I get the error in line 3:
  23. >   Can not convert Cat * to Animal *
  24. > But if I define
  25. >    class Cat:public Animal{...
  26. > everything is fine.
  27. > Can anyone tell me why?
  28. >     [clip]
  29.  
  30. The default access for classes is private.
  31. p = &a;     // asks for an implicit conversion.
  32.  
  33. "Pointers and references to derived classes can be implicitly converted to
  34. pointers and references 
  35. to their base classes if there is an accessible, unambiguous base class."
  36.  
  37. Key word in this case is accessible (i.e., at least, not private.)
  38.  
  39.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  40.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  41.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  42.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  43.